package com.neborosoft.FlyingDog.android.playing.modes;

import android.app.Activity;
import com.neborosoft.FlyingDog.android.gui.adapters.listview.PlayListsAdapter;
import com.neborosoft.FlyingDog.android.gui.adapters.listview.SongsPlayListAdapter;
import com.neborosoft.FlyingDog.logic.database.AudioDataManager;
import com.neborosoft.FlyingDog.logic.database.content.identified.ArtSize;
import com.neborosoft.FlyingDog.logic.database.content.identified.Audio;
import com.neborosoft.FlyingDog.logic.database.content.identified.PlayList;
import com.neborosoft.FlyingDog.logic.search.suggestions.SuggestionsProvider;
import com.neborosoft.android.gui.adapters.listview.ViewArrayAdapter;
import com.neborosoft.android.gui.wrappers.ListViewSwitcher;
import com.neborosoft.lib.API_8.Collections;
import com.neborosoft.lib.FlyingDogRuntimeException;
import com.neborosoft.lib.collections.tree.NTree;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 01.12.12
 * Time: 21:44
 * To change this template use File | Settings | File Templates.
 */
public class PlayListsMode extends TreeStructuredPlayingMode {
    private static final int PLAY_LISTS_LEVEL = 0;
    private static final int SONGS_LEVEL = 1;
    private static final ArtSize PLAY_LIST_FOLDER_ART_SIZE = ArtSize.SMALL;

    private PlayListsAdapter.ImagesGetter playListFolderImagesGetter = new PlayListsAdapter.ImagesGetter() {
        @Override
        public String[] getPlayListImagesUrls(PlayList playList, int arrayLength) {
            String[] result = new String[arrayLength];
            if(!playList.isLocal()){
                return result;
            }

            Iterable<Audio> tracks = AudioDataManager.getInstance().getTracksOfPlayList(playList);

            int i = 0;
            for(Audio track : tracks){
                if(i >= result.length){
                    break;
                }

                if(track == null){
                    continue;
                }

                String albumArtUrl = track.getArtUrl(PLAY_LIST_FOLDER_ART_SIZE);
                if(albumArtUrl == null){
                    continue;
                }
                else{
                    result[i++] = albumArtUrl;
                }
            }

            return result;
        }
    };

    @Override
    protected Class<? extends ViewArrayAdapter> getListViewAdapterClass(int level) {
        switch(level){
            case PLAY_LISTS_LEVEL: return PlayListsAdapter.class;
            case SONGS_LEVEL: return SongsPlayListAdapter.class;
            default: throw new FlyingDogRuntimeException("invalid level request");
        }
    }

    @Override
    protected int getListViewResourceIdForLevel(int level) {
        return Constants.listViewSwitcherResource;
    }

    public PlayListsMode(Activity activity, ListViewSwitcher listView, NTree tree) {
        super(activity, listView, tree);
        PlayListsAdapter playListsAdapter = (PlayListsAdapter)getAdapterForLevel(PLAY_LISTS_LEVEL);
        playListsAdapter.setImagesGetter(playListFolderImagesGetter);
    }

    @Override
    public Object performSearch(String query) {
        return Collections.emptyList();
    }

    @Override
    protected SuggestionsProvider createSuggestionsProvider() {
        return null;
    }
}
